package to52.android.screens;

import to52.android.adapters.PatientDetailsAdapter;
import to52.android.database.RetreiveData;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import fr.android.ui.SliceNavigator;
import fr.utbm.Patient;
import fr.utbm.Sensor;

public class PatientDetailsList extends SliceNavigator {

	private Patient currentPatient;
	
	private fr.utbm.PatientList patients;
	private int currentIndex;
	
	private ListView currentPatientDetailsView;
	
	@Override
	public void afterCreate() {
		currentPatientDetailsView = (ListView) findViewById(R.id.navigation_item_list);
		
		RetreiveData rd = new RetreiveData(this);
		patients = rd.getPatients();

		Patient p = rd.getPatient(getIntent().getExtras().getInt("current_patient_id"));
		setCurrentPatientIndex(patients.indexOf(p));
		
		rd.close();
		
		currentPatientDetailsView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            	Intent i = new Intent();
            	i.setClass(getApplicationContext(), PlotView.class);
            	
            	// on passe le type de capteur dont on veut le détail
            	i.putExtra("capteur_id", ((Sensor)arg0.getItemAtPosition(arg2)).getType().getID());

            	i.putExtra("current_patient_id", currentPatient.getId());
            	i.putExtra("current_patient_name", currentPatient.getName());
        		startActivity(i);	
			}
		
		});
	}
	
	private void setCurrentPatientIndex(int index) {
		currentIndex = index;
		
		currentPatient = patients.get(index);
		setTitle(currentPatient.getName());
		currentPatientDetailsView.setAdapter(new PatientDetailsAdapter(getBaseContext(), currentPatient)); 
		
		currentIndex = patients.indexOf(currentPatient);
		setSliceLeftEnable(currentIndex != 0);
		setSliceRightEnable(currentIndex != patients.size() - 1);
	}
	
	@Override
	public void onSliceLeft() {
		playLeftAnimation();
		setCurrentPatientIndex(currentIndex - 1);
	}

	@Override
	public void onSliceRight() {
		playRightAnimation();
		setCurrentPatientIndex(currentIndex + 1);
	}

}
